package org.shan.lf.sever.iml;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mlnx.common.entity.BaseEntity;
import org.shan.spring.base.utils.ServletUtils;
import org.shan.spring.mybatis.service.BaseMybatisService;

import java.util.Date;
import java.util.Map;

/**
 * Created by amanda.shan on 2019/7/15.
 */
public class BaseService extends BaseMybatisService {

    public String getUsername() {

        Object username = ServletUtils.getAttribute("username");
        if (username != null) {
            return username.toString();
        } else {
            return null;
        }
    }

    public Integer getUserId(){
        return 0;
    }

    /**
     * 包装 插入创建者和创建时间
     *
     * @param t
     * @param <T>
     * @return
     */
    public <T extends BaseEntity> T wrapperCreater(T t) {

        Object username = ServletUtils.getAttribute("username");
        if (username != null) {
            t.setCreater(username.toString());
        }
        t.setCreateTime(new Date());

        return t;
    }

    /**
     * 包装 插入创建者和创建时间
     *
     * @param t
     * @param <T>
     * @return
     */
    public <T extends BaseEntity> T wrapperEditor(T t) {

        String username = ServletUtils.getAttribute("username");
        if (username != null) {
            t.setEditor(username.toString());
        }
        t.setEditTime(new Date());

        return t;
    }

    @Override
    public QueryWrapper appendState(QueryWrapper wrapper) {
        return super.appendState(wrapper);
    }

    @Override
    public UpdateWrapper logicDeleteByIdWrapper(Integer id) {
        return super.logicDeleteByIdWrapper(id);
    }

    @Override
    public UpdateWrapper logicDeleteByMapWrapper(Map<String, Object> map) {
        return super.logicDeleteByMapWrapper(map);
    }

    @Override
    public UpdateWrapper logicDeleteWrapper(UpdateWrapper wrapper) {
        return super.logicDeleteWrapper(wrapper);
    }
}
